//
//  SDShowPersonalInfo.m
//  SuperDoctor
//
//  Created by fdq on 16/7/7.
//  Copyright © 2016年 SD. All rights reserved.
//

#import "SDShowPersonalInfo.h"
#import "SDPersonInfoModel.h"
#import "SDShowInfoCell.h"
#import "SDEditPersonInfoController.h"

#define kFilePath(fileName) ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName])


@interface SDShowPersonalInfo ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)NSMutableArray * dataArray;



@property (nonatomic,strong)SDPersonInfoModel * model;

@property (nonatomic,weak)UITableView * tableView;

@end

@implementation SDShowPersonalInfo

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.view.backgroundColor = [UIColor whiteColor];
	
	[self setupUI];

}

- (void)setupUI{
	
	UITableView * tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
	
//	[tableView registerClass:[SDShowInfoCell class] forCellReuseIdentifier:@"cell"];
	
	tableView.delegate = self;
	
	tableView.dataSource = self;

	tableView.rowHeight = UITableViewAutomaticDimension;
	
	tableView.estimatedRowHeight = 100;
	
	self.tableView = tableView;
	[self.view addSubview:tableView];
	
	
	
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	
	NSLog(@"---%ld",self.dataArray.count);
	return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	
	SDShowInfoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
	
	if(!cell){
	
		cell = (SDShowInfoCell *)[[SDShowInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
		
		cell.indexPath = indexPath;

	}
	
	NSLog(@"---%ld",indexPath.row);
	
	SDPersonInfoModel *model = self.dataArray[indexPath.row];
	
	cell.model = model;

	
//	cell.index = (int)indexPath.row;
	
	return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
	
	UIImageView * topImg = [[UIImageView alloc]init];
	
	topImg.image = [UIImage imageNamed:@"h2"];
	
	topImg.userInteractionEnabled = YES;
	
	//设置左侧返回按钮
	UIButton * leftButton = [[UIButton alloc]init];
	
	[leftButton setTitle:@"返回" forState:UIControlStateNormal];
	
	[topImg addSubview:leftButton];
	
	leftButton.userInteractionEnabled = YES;
	
	[leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
	
		make.top.mas_offset(10);
		
		make.left.mas_offset(10);
	}];
	
	[leftButton addTarget:self action:@selector(clickBackButton) forControlEvents:UIControlEventTouchUpInside];
	
	//设置图片
	UIImageView * iconImg = [[UIImageView alloc]init];
	
	iconImg.image = [UIImage imageNamed:@"illness_img_person.png"];
	
	[topImg addSubview:iconImg];
	
	[iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
	
		make.centerY.mas_equalTo(topImg.mas_bottom);
		
		make.left.mas_offset(10);
		
		make.height.mas_equalTo(70);
		
		make.width.mas_equalTo(70);
	}];
	
	
	return topImg;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
	
	UIView * bottomView = [[UIView alloc]init];
	
	UIButton * firstButton = [[UIButton alloc]init];
	
	[firstButton setBackgroundImage:[UIImage imageNamed:@"xx"] forState:UIControlStateNormal];
	
	[bottomView addSubview:firstButton];
	
	CGFloat margin = (self.view.frame.size.width - 60 * 5)/6;
	
//	NSLog(@"%f",margin);
	
	[firstButton mas_makeConstraints:^(MASConstraintMaker *make) {
		
		make.centerY.mas_equalTo(bottomView.mas_centerY);
				
		make.height.mas_equalTo(60);
		
		make.width.mas_equalTo(60);
		
		make.centerX.mas_equalTo(bottomView.mas_centerX);
		
	}];
	
	UIButton * secondButton = [[UIButton alloc]init];

	[secondButton setBackgroundImage:[UIImage imageNamed:@"gt"] forState:UIControlStateNormal];

	[bottomView addSubview:secondButton];
	
	[secondButton mas_makeConstraints:^(MASConstraintMaker *make) {
		
		make.centerY.mas_equalTo(bottomView.mas_centerY);
		
		make.left.mas_equalTo(firstButton.mas_right).mas_offset(margin);
		
		make.height.mas_equalTo(60);
		
		make.width.mas_equalTo(60);
		
	}];

	UIButton * thirdButton = [[UIButton alloc]init];
	
	[thirdButton setBackgroundImage:[UIImage imageNamed:@"xx"] forState:UIControlStateNormal];
	
	[bottomView addSubview:thirdButton];
	
	[thirdButton mas_makeConstraints:^(MASConstraintMaker *make) {
		
		make.centerY.mas_equalTo(bottomView.mas_centerY);
		
		make.left.mas_equalTo(secondButton.mas_right).mas_offset(margin);
		
		make.height.mas_equalTo(60);
		
		make.width.mas_equalTo(60);
	}];

	UIButton * forthButton = [[UIButton alloc]init];
	
	[forthButton setBackgroundImage:[UIImage imageNamed:@"sj"] forState:UIControlStateNormal];
	
	[bottomView addSubview:forthButton];
	
	[forthButton mas_makeConstraints:^(MASConstraintMaker *make) {
		
		make.centerY.mas_equalTo(bottomView.mas_centerY);
		
		make.right.mas_equalTo(firstButton.mas_left).mas_offset(-margin);
		
		make.height.mas_equalTo(60);
		
		make.width.mas_equalTo(60);
		
	}];
	
	UIButton * fifthButton = [[UIButton alloc]init];
	
	[fifthButton setBackgroundImage:[UIImage imageNamed:@"sj"] forState:UIControlStateNormal];
	
	[bottomView addSubview:fifthButton];
	
	[fifthButton mas_makeConstraints:^(MASConstraintMaker *make) {
		
		make.centerY.mas_equalTo(bottomView.mas_centerY);
		
		make.right.mas_equalTo(forthButton.mas_left).mas_offset(-margin);
		
		make.height.mas_equalTo(60);
		
		make.width.mas_equalTo(60);
		
	}];
	
	return bottomView;
}

//点击cell,页面跳转到可编辑页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	
	
	
	
	SDEditPersonInfoController * editVC = [[SDEditPersonInfoController alloc]init];
	
	editVC.textBlock = ^{
		
		dispatch_async(dispatch_get_global_queue(0, 0), ^{
			//保存修改数据
			[NSKeyedArchiver archiveRootObject:self.dataArray toFile:kFilePath(@"user.data")];
		});
		
		[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
	};
	
	editVC.model = self.dataArray[indexPath.row];
	
	[self presentViewController:editVC animated:YES completion:nil];
}



//设置组头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
	
	return 220;
}

//组尾高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
	
	return 100;
}

- (BOOL)prefersStatusBarHidden{
	
	return YES;
}

//设置行高
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//	
//	if(indexPath.row == 0){
//		
//		return 70;
//	}
//}

//- (CGFloat)calculateHeight:(SDPersonInfoModel *)model{
//	
//	CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
//	
//	CGSize maxSIze = CGSizeMake(screenWidth-100, MAXFLOAT);
//	
//	CGFloat heightH =  [model.information boundingRectWithSize:maxSIze options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT(14)} context:nil].size.height;
//	
////		NSLog(@"height = %.2f",heightH);
//	
////	self.height = heightH+10+5;
//	
//	return heightH+10+5+10+10;
//}

//监听点击按钮方法
- (void)clickBackButton{
	
	[self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - 懒加载

- (NSArray *)dataArray{
	
	
		if (nil == _dataArray) {
			
			_dataArray = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"user.data")];
			
			if (_dataArray == nil) {
//				_dataArray = [[NSMutableArray alloc]init];
				// 1. 获取文件路径
				NSString *path = [[NSBundle mainBundle] pathForResource:@"Person.plist" ofType:nil];
				
				// 2. 读取文件内容到临时数组
				NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
				
				
				// 3. 创建可变数组
				NSMutableArray *mutable = [NSMutableArray array];
				
				// 4/ 遍历 tempArray 中的字典, 给转为模型AppModel
				for (NSDictionary *dict in tempArray) {
					// 4.1 实例化一个模型对象
					//            AppModel *appModel = [[AppModel alloc] initWithDict:dict];
					
					SDPersonInfoModel *appModel = [SDPersonInfoModel modelWithDict:dict];
					
					
					// 4.3 把对象添加到可变数组中
					[mutable addObject:appModel];
				
				}
				
				
				// 5. 不要忘记, 把可变数组, 赋值给 _dataArray
				_dataArray = mutable;
			}
			
			
			
		}

	
	return _dataArray;
}

@end
